package com.wc.算法提高课.D第四章_高级数据结构.AC自动机.修复DNA;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/29 1:13
 * @description https://www.acwing.com/problem/content/1055/
 */
public class Main {
    /**
     * AC自动机 + DP
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, idx = 0, INF = 0x3f3f3f3f;
    static int[][] tr = new int[N][4], f = new int[N][N];
    static int[] ne = new int[N], q = new int[N], cnt = new int[N];
    static char[] s;
    static int n, m;

    public static void main(String[] args) {
        int T = 1;
        while (true) {
            n = sc.nextInt();
            if (n == 0) break;
            for (int i = 0; i < tr.length; i++) {
                Arrays.fill(tr[i], 0);
                ne[i] = 0;
                cnt[i] = 0;
            }
            idx = 0;
            for (int i = 1; i <= n; i++) {
                s = sc.next().toCharArray();
                insert();
            }
            build();
            for (int i = 0; i < f.length; i++) Arrays.fill(f[i], INF);
            f[0][0] = 0;
            s = (" " + sc.next()).toCharArray();
            m = s.length - 1;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j <= idx; j++) {
                    for (int k = 0; k < 4; k++) {
                        int t = k == get(s[i + 1]) ? 0 : 1;
                        // 当前节点
                        int p = tr[j][k];
                        if (cnt[p] == 0)
                            f[i + 1][p] = Math.min(f[i][j] + t, f[i + 1][p]);
                    }
                }
            }
            int res = INF;
            for (int i = 0; i <= idx; i++) res = Math.min(res, f[m][i]);
            out.printf("Case %d: %d\n", T++, res == INF ? -1 : res);
        }
        out.flush();
    }

    static void build() {
        int hh = 1, tt = 0;
        for (int i = 0; i < 4; i++) {
            if (tr[0][i] > 0) q[++tt] = tr[0][i];
        }

        while (hh <= tt) {
            int t = q[hh++];
            for (int i = 0; i < 4; i++) {
                int p = tr[t][i];
                if (p == 0) tr[t][i] = tr[ne[t]][i];
                else {
                    ne[p] = tr[ne[t]][i];
                    q[++tt] = p;
                    // 因为他本身可能包含完整的其他序列，例如abcd 包含bc
                    cnt[p] |= cnt[ne[p]];
                }
            }
        }
    }

    static void insert() {
        int p = 0;
        for (int c : s) {
            int t = get(c);
            if (tr[p][t] == 0) tr[p][t] = ++idx;
            p = tr[p][t];
        }
        cnt[p] = 1;
    }

    static int get(int c) {
        if (c == 'A') return 0;
        if (c == 'T') return 1;
        if (c == 'C') return 2;
        return 3;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
